package com.google.android.exoplayer2.video;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.EGL14;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;
import android.view.Surface;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.EGLSurfaceTexture;
import o.amm;
import o.amz;
import o.anr;

@TargetApi(17)
public final class DummySurface extends Surface {
    private static boolean ˊ;
    private static int ॱ;
    public final boolean ˋ;
    private boolean ˎ;
    private final e ˏ;

    static class e extends HandlerThread implements Callback {
        @Nullable
        private Error ˊ;
        @Nullable
        private RuntimeException ˋ;
        private EGLSurfaceTexture ˎ;
        @Nullable
        private DummySurface ˏ;
        private Handler ॱ;

        public e() {
            super("dummySurface");
        }

        public DummySurface ˎ(int i) {
            Object obj = null;
            start();
            this.ॱ = new Handler(getLooper(), this);
            this.ˎ = new EGLSurfaceTexture(this.ॱ);
            synchronized (this) {
                this.ॱ.obtainMessage(1, i, 0).sendToTarget();
                while (this.ˏ == null && this.ˋ == null && this.ˊ == null) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        obj = 1;
                    }
                }
            }
            if (obj != null) {
                Thread.currentThread().interrupt();
            }
            if (this.ˋ != null) {
                throw this.ˋ;
            } else if (this.ˊ == null) {
                return (DummySurface) amm.ॱ(this.ˏ);
            } else {
                throw this.ˊ;
            }
        }

        public void ˊ() {
            amm.ॱ(this.ॱ);
            this.ॱ.sendEmptyMessage(2);
        }

        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 1:
                    try {
                        ˊ(message.arg1);
                        synchronized (this) {
                            notify();
                        }
                        break;
                    } catch (Throwable e) {
                        amz.ˊ("DummySurface", "Failed to initialize dummy surface", e);
                        this.ˋ = e;
                        synchronized (this) {
                            notify();
                            break;
                        }
                    } catch (Throwable e2) {
                        amz.ˊ("DummySurface", "Failed to initialize dummy surface", e2);
                        this.ˊ = e2;
                        synchronized (this) {
                            notify();
                            break;
                        }
                    } catch (Throwable th) {
                        synchronized (this) {
                            notify();
                        }
                    }
                case 2:
                    try {
                        ॱ();
                        break;
                    } catch (Throwable e22) {
                        amz.ˊ("DummySurface", "Failed to release dummy surface", e22);
                        break;
                    } finally {
                        quit();
                    }
            }
            return true;
        }

        private void ˊ(int i) {
            amm.ॱ(this.ˎ);
            this.ˎ.ˏ(i);
            this.ˏ = new DummySurface(this, this.ˎ.ॱ(), i != 0);
        }

        private void ॱ() {
            amm.ॱ(this.ˎ);
            this.ˎ.ˋ();
        }
    }

    public static synchronized boolean ˏ(Context context) {
        boolean z = true;
        synchronized (DummySurface.class) {
            if (!ˊ) {
                ॱ = anr.ˋ < 24 ? 0 : ॱ(context);
                ˊ = true;
            }
            if (ॱ == 0) {
                z = false;
            }
        }
        return z;
    }

    public static DummySurface ˎ(Context context, boolean z) {
        boolean z2;
        int i = 0;
        ˎ();
        if (!z || ˏ(context)) {
            z2 = true;
        } else {
            z2 = false;
        }
        amm.ˏ(z2);
        e eVar = new e();
        if (z) {
            i = ॱ;
        }
        return eVar.ˎ(i);
    }

    private DummySurface(e eVar, SurfaceTexture surfaceTexture, boolean z) {
        super(surfaceTexture);
        this.ˏ = eVar;
        this.ˋ = z;
    }

    public void release() {
        super.release();
        synchronized (this.ˏ) {
            if (!this.ˎ) {
                this.ˏ.ˊ();
                this.ˎ = true;
            }
        }
    }

    private static void ˎ() {
        if (anr.ˋ < 17) {
            throw new UnsupportedOperationException("Unsupported prior to API level 17");
        }
    }

    @TargetApi(24)
    private static int ॱ(Context context) {
        if (anr.ˋ < 26 && ("samsung".equals(anr.ˏ) || "XT1650".equals(anr.ˊ))) {
            return 0;
        }
        if (anr.ˋ < 26 && !context.getPackageManager().hasSystemFeature("android.hardware.vr.high_performance")) {
            return 0;
        }
        String eglQueryString = EGL14.eglQueryString(EGL14.eglGetDisplay(0), 12373);
        if (eglQueryString == null || !eglQueryString.contains("EGL_EXT_protected_content")) {
            return 0;
        }
        return eglQueryString.contains("EGL_KHR_surfaceless_context") ? 1 : 2;
    }
}
